package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealDishVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
@Slf4j
@Validated
@Tag(name = "C端套餐管理")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "根据分类id查询套餐")
    @Cacheable(value = "sky-take-out:setmeal:list",key = "#categoryId") //key:sky-take-out:setmealCache::100
    public Result<List<Setmeal>> list(@RequestParam @NotNull(message = "分类id不能为空") Long categoryId) {
        log.info("分类条件id{}", categoryId);
        List<Setmeal> setmealList = setmealService.getSetmealByCategoryId(categoryId);
        if (CollectionUtils.isNotEmpty(setmealList)){
            return Result.success(setmealList);
        }else {
            //此分类下没有套餐
            return Result.error(MessageConstant.CATEGORY_UNDER_NOSETMEALS);
        }
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @Operation(summary = "根据套餐id查询包含的菜品")
    public Result<List<SetmealDishVO>> getSetmealDishById(@PathVariable Long id) {
        log.info("根据套餐id查询包含的菜品{}", id);
        List<SetmealDishVO> setmealDishVOList = setmealService.getSetmealDishBySetmealId(id);
        if (CollectionUtils.isNotEmpty(setmealDishVOList)){
            return Result.success(setmealDishVOList);
        }else {
            //此套餐下没有菜品
            return Result.error(MessageConstant.SETMEAL_UNDER_NODISHES);
        }
    }
}
